package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.common.Reflection;
import com.itheima.reggie.dto.PermissionsDto;
import com.itheima.reggie.entity.Children;
import com.itheima.reggie.entity.Permissions;
import com.itheima.reggie.service.PermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/permissions")
@RestController
@Slf4j
public class PermissionsController {
    @Autowired
    private PermissionsService permissionsService;

    /**
     * 获取所有权限信息
     * @return
     */
    @GetMapping("/allPermissions")
    public R<List<Children>> allPermissions(){
        //获取该员工的所有可以访问的Controller下的方法信息
        List<Children> childrenList = Reflection.getReflections();
        return R.success(childrenList);
    }

    /**
     * 获取所选用户的权限信息
     * @param empId
     * @return
     */
    @GetMapping("/user")
    public R<List<Permissions>> userPermissions(Long empId){
        List<Permissions> list = permissionsService.userPermissions(empId);
        return R.success(list);
    }

    /**
     * 保存设置的权限信息，先删除，在添加
     * @param permissionsDto 保存的权限 ,被设置权限员工的id， 封装的实体类
     * @return
     */
    @PostMapping
    public R<String> savePermissions(@RequestBody PermissionsDto permissionsDto){
        String savePermissions = permissionsService.savePermissions(permissionsDto);
        return R.success(savePermissions);
    }
}
